package BeadSort;
import java.util.Scanner;

public class BeadSort implements Runnable{
	public int[][] abacus;
	
	public void run()
	{
		
	}
	
	public BeadSort()
	{
		
	}

	protected void printArray(int[] arr)
	{
		System.out.print("[");
		for(int x=0; x<arr.length; x++)
		{
			if(x!=arr.length-1)
				System.out.print(arr[x]+", ");
			else
				System.out.print(arr[x]);
		}
		System.out.println("]");
	}
	
	private int largestElement(int[] arr)
	{
		int max=0;
		for(int x=0; x<arr.length; x++)
		{
			if (max<arr[x])
				max=arr[x];
		}
		return max;
	}
	
	protected void setAbacusColumn(int x, int[] arr)
	{
		this.abacus[x]=arr;
	}
	
	private int[] beadCount()
	{
		int count=0;
		int[] arr=new int[abacus[0].length];
		
		for(int x=0; x<this.abacus[0].length; x++)
		{
			for(int y=0; y<this.abacus.length; y++)
			{
				if(this.abacus[y][x]==1)
					count++;
			}
			arr[x]=count;
			count=0;
		}
		return arr;
	}
	
	private void gravity()
	{
		int x=0;//remove this when uncommenting below.
		//for(int x=0; x<abacus.length; x++)
		{			
			Thread t=new Thread(new Grav(this.abacus[x],x));
			t.start();
		}
		
		
		/*
		int beadcount=0;
		
		for(int x=0; x<arr.length; x++)
		{
			if(arr[x]==1)
				beadcount++;
			arr[x]=0;
		}
		for(int x=arr.length-1; x>=0; x--)
		{
			if(beadcount>0)
			{
				arr[x]=1;
				beadcount--;
			}
		}
		*/
	}
	
	private void beadSort(int[] arr)
	{
		int large=largestElement(arr);
		abacus=new int[large][arr.length]; 
		
		printArray(arr);
		System.out.println("ARR^");
		for(int x=0; x<large; x++)
		{
			for(int y=0; y<arr.length; y++)
			{
				if(x<arr[y])
				{
					abacus[x][y]=1;
				}
			}
			printArray(abacus[x]);
		}
		
		System.out.println();
		/*
		for(int x=0; x<abacus.length; x++)
		{
			gravity();
			printArray(abacus[x]);
		}*/
		int x=0;//remove this when uncommenting below.
		//for(int x=0; x<abacus.length; x++)
		{			
			Thread t=new Thread(new Grav(this.abacus[x],x));
			t.start();
		}
		
		arr=beadCount();
		System.out.println();
		printArray(arr);
	}
	
	public static void main (String[] args)
	{
		Scanner s=new Scanner(System.in);
		int input=0;
		System.out.println("How many numbers do you want to sort?");
		input=s.nextInt();
		int[] arr=new int[input];
		
		System.out.println("Now, enter the values you wish to sort.");
		for(int x=0; x<arr.length; x++)
		{
			input=s.nextInt();
			arr[x]=input;
		}
		BeadSort beady=new BeadSort();
		beady.beadSort(arr);
	}
}
